<template>
    <div>
        <h2>优惠券添加</h2>
        <table class="table table-bordered">
            <tr>
                <td align="right">优惠券类型</td>
                <td>
                    <select v-model="state.dTid" @change="gettype">
                        <option value="">请选择</option>
                        <option :value="item.dtId" v-for="item in type">{{ item.name }}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="right">优惠券名称</td>
                <td><input type="text" v-model="state.dName"  ></td>
            </tr>
            <tr>
                <td align="right">门店</td>
                <td>
                    <select v-model="state.sid" @change="getstore">
                        <option value="">请选择</option>
                        <option :value="item.sId" v-for="item in store">{{ item.sName }}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td align="right">优惠券面值</td>
                <td><input type="text" v-model="state.money"></td>
            </tr>
            <tr>
                <td align="right">优惠券最低消费</td>
                <td><input type="text" v-model="state.minPrice"></td>
            </tr>
            <tr>
                <td align="right">优惠券有效期限</td>
                <td><input type="text" v-model="state.days"></td>
            </tr>
            <tr>
                <td align="right"></td>
                <td><input type="button" value="添加" @click="add"></td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, toRefs } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';

let router = useRouter()
let data: any = reactive({
    state: {
        sid: "",
        dName: "",
        dTid: "",
        money: "",
        minPrice: "",
        days: "",
    },
    store: '',
    type: ''
})
let { state, store, type } = toRefs(data)
onMounted(() => {
    getstore()
    gettype()
})
const getstore = () => {
    let obj = {
name:'',
phone:'',
        index: 1,
        size: 100
    }
    axios({
        url: '/api/Store/GetStores',
        method: 'get',
        data: obj
    })
        .then(res => {
            console.log(res)
            store.value = res.data.data
        })
        .catch(err => {
            console.log(err)
        })
}
const gettype = () => {

    axios({
        url: '/api/Discount/GetTypes',
        method: 'get',
    })
        .then(res => {
            console.log(res)
            type.value = res.data.data
        })
        .catch(err => {
            console.log(err)
        })
}

const add=()=>{
    if(state.value.sid==""){
        alert("类型不能为空")
        return;
    }
    if(state.value.dName==""){
        alert("名称不能为空")
        return;
    }
    if(state.value.dTid==""){
        alert("门店不能为空")
        return;
    }
    if(state.value.money==""){
        alert("面值不能为空")
        return;
    }
    if(state.value.minPrice==""){
        alert("最低消费额不能为空")
        return;
    }
    if(state.value.days==""){
        alert("有效期限不能为空")
        return;
    }
    let obj={
        sid: state.value.sid,
        dName: state.value.dName,
        dTid: state.value.dTid,
        money: state.value.money,
        minPrice: state.value.minPrice,
        days: state.value.days,
    }
    axios({
        url:'/api/Discount/DiscountAdd',
        method:'post',
        data:obj
    })
    .then(res=>{
        console.log(res)
        if(res.data.data==1){
            alert("添加成功")
        
        }
        else{
            alert("添加失败")
            return;
        }
    })
    .catch(err=>{
        console.log(err)
    })
}

</script>

<style scoped></style>